package com.zzyl.service.impl;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.DeviceDataPageReqDto;
import com.zzyl.mapper.DeviceDataMapper;
import com.zzyl.service.DeviceDataService;
import com.zzyl.utils.ObjectUtil;
import com.zzyl.vo.DeviceDataGraphVo;
import com.zzyl.vo.DeviceDataVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DeviceDataServiceImpl implements DeviceDataService {

    @Autowired
    private DeviceDataMapper deviceDataMapper;

    /**
     * 分页条件查询
     * @param deviceDataPageReqDto
     * @return
     */
    @Override
    public PageResponse<DeviceDataVo> selectByPage(DeviceDataPageReqDto deviceDataPageReqDto) {
        PageHelper.startPage(deviceDataPageReqDto.getPageNum(),deviceDataPageReqDto.getPageSize());

        //转换时间的类型
        Long startTime = deviceDataPageReqDto.getStartTime();
        Long endTime = deviceDataPageReqDto.getEndTime();

        deviceDataPageReqDto.setStartDate(ObjectUtil.isEmpty(startTime) ? null: LocalDateTimeUtil.of(startTime));
        deviceDataPageReqDto.setEndDate(ObjectUtil.isEmpty(endTime) ? null: LocalDateTimeUtil.of(endTime));

        Page<DeviceDataVo> page = deviceDataMapper.selectByPage(deviceDataPageReqDto);
        return PageResponse.of(page,DeviceDataVo.class);
    }

    /**
     * 根据日期查询某一个设备的某一个属性的数据
     *
     * @param iotId
     * @param functionId
     * @param startTime
     * @param endTime
     * @return
     */
    @Override
    public List<DeviceDataGraphVo> queryDeviceDataListByDay(String iotId, String functionId, Long startTime, Long endTime) {

        //[{'12:00':100},{'13:00':100},{'14:00':100}]
        List<DeviceDataGraphVo> list = deviceDataMapper.queryDeviceDataListByDay(iotId, functionId, LocalDateTimeUtil.of(startTime), LocalDateTimeUtil.of(endTime));

        //[{'00:00',0},{01:00',0},{02:00',0}...{23:00',0}]
        List<DeviceDataGraphVo> allList = DeviceDataGraphVo.dayInstance(LocalDateTimeUtil.of(startTime));

        //可以先把list集合转换为map结合(时间:value)
        Map<String, Double> map = list.stream().collect(Collectors.toMap(DeviceDataGraphVo::getDateTime, DeviceDataGraphVo::getDataValue));

        //遍历每一个时间 然后如果在map中找到了数据 那么就覆盖原来的默认值
        allList.stream().map(vo -> {
            Double value = map.get(vo.getDateTime());
            if(ObjectUtil.isNotEmpty(value)){
                vo.setDataValue(value);
            }
            return vo;
        }).collect(Collectors.toList());

        return allList;
    }

    /**
     * 根据周查询某一个设备的某一个属性的一天的平均数据
     *
     * @param iotId
     * @param functionId
     * @param startTime
     * @param endTime
     * @return
     */
    @Override
    public List<DeviceDataGraphVo> queryDeviceDataListByWeek(String iotId, String functionId, Long startTime, Long endTime) {
        List<DeviceDataGraphVo> list = deviceDataMapper
                .queryDeviceDataListByWeek(iotId, functionId, LocalDateTimeUtil.of(startTime), LocalDateTimeUtil.of(endTime));

        List<DeviceDataGraphVo> allList = DeviceDataGraphVo.weekInstance(LocalDateTimeUtil.of(startTime));

        Map<String, Double> map = list.stream().collect(Collectors.toMap(DeviceDataGraphVo::getDateTime, DeviceDataGraphVo::getDataValue));

        allList.stream().map(vo -> {
            String key = vo.getDateTime();
            Double value = map.get(key);
            if(ObjectUtil.isNotEmpty(value)){
                vo.setDataValue(value);
            }

            return vo;
        }).collect(Collectors.toList());

        return allList;
    }
}
